import pygame, os, sys, math, random

pygame.init()

from Block import Block
from Player import Player
from HardBlock import HardBlock
from Enemy import Enemy
from Background import Background
from Menu import Menu
from button import Button

clock = pygame.time.Clock()

width = 800
height = 600
size = width, height

blocksize = [50,50]
playersize = [30,30]

screen = pygame.display.set_mode(size)
bgColor = r,g,b = 0,0,0

blocks = pygame.sprite.Group()
hardBlocks = pygame.sprite.Group()
enemies = pygame.sprite.Group()
backgrounds = pygame.sprite.Group()
players = pygame.sprite.Group()
menus = pygame.sprite.Group()
levelselect = pygame.sprite.Group()
buttons = pygame.sprite.Group()
all = pygame.sprite.OrderedUpdates()

Player.containers = (all, players)
Block.containers = (all, blocks)
HardBlock.containers = (all, hardBlocks)
Enemy.containers = (all, enemies)
Menu.containers = (all)
Background.containers = (all)
Button.containers = (all, buttons)



bg = Background("rsc/bg/mainbg.png", size)

def loadLevel(level):
    f = open(level+".lvl", 'r')
    lines = f.readlines()
    f.close()
    
    newlines = []
    
    for line in lines:
        newline = ""
        for c in line:
            if c != "\n":
                newline += c
        newlines += [newline]
    
    for y, line in enumerate(newlines):
        for x, c in enumerate(line):
            if c == "#":
                HardBlock("rsc/blocks/black.png", 
                      [(x*blocksize[0])+blocksize[0]/2, (y*blocksize[1])+blocksize[1]/2], 
                      blocksize)
            elif c == "p":
                Block("rsc/blocks/purple.png", 
                      [(x*blocksize[0])+blocksize[0]/2, (y*blocksize[1])+blocksize[1]/2], 
                      blocksize)
            elif c == "b":
                Block("rsc/blocks/blue.png", 
                      [(x*blocksize[0])+blocksize[0]/2, (y*blocksize[1])+blocksize[1]/2], 
                      blocksize)
    
    f = open(level+".tng", 'r')
    lines = f.readlines()
    f.close()
    
    newlines = []
    
    for line in lines:
        newline = ""
        for c in line:
            if c != "\n":
                newline += c
        newlines += [newline]
    
    for y, line in enumerate(newlines):
        for x, c in enumerate(line):
            if c == "@":
                player = Player([(x*blocksize[0])+blocksize[0]/2, (y*blocksize[1])+blocksize[1]/2],
                     playersize,
                     size)
            elif c == "e":
                Enemy("rsc/enemy/red guy.png", 
                      [(x*blocksize[0])+blocksize[0]/2, (y*blocksize[1])+blocksize[1]/2], 
                      playersize)
    for each in all.sprites():
        each.fixLocation(player.offsetx, player.offsety)
     
    

levels = ["rsc/levels/Level 1",
          "rsc/levels/level 2",
          "rsc/levels/level 3",
          "rsc/levels/level 4",
          "rsc/levels/level 5",
          "rsc/levels/level 6"]
level = 0
loadLevel(levels[level])
player1 = players.sprites()[0]

while True:
    start = True
    game = False
    start2 = False
    Menu(["rsc/bg/Main Menu..png",
            "rsc/bg/Main Menu2.png",
            "rsc/bg/Main Menu3.png",
            "rsc/bg/Main Menu4.png",
            "rsc/bg/Main Menu5.png",
            "rsc/bg/Main Menu6.png",
            "rsc/bg/Main Menu7.png",
            "rsc/bg/Main Menu8.png",
            "rsc/bg/Main Menu9.png",
            "rsc/bg/Main Menu8.png",
            "rsc/bg/Main Menu7.png",
            "rsc/bg/Main Menu6.png",
            "rsc/bg/Main Menu5.png",
            "rsc/bg/Main Menu4.png",
            "rsc/bg/Main Menu3.png",
            "rsc/bg/Main Menu2.png",
            "rsc/bg/Main Menu..png"], size)
    
    
    
    while start and not game and not start2:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    start = False
                    game = False
                    start2 = True
        
        all.update(size,
                   player1.speedx, 
                   player1.speedy, 
                   player1.scrollingx, 
                   player1.scrollingy,
                   player1.realx,
                   player1.realy)
        
        dirty = all.draw(screen)
        pygame.display.update(dirty)
        pygame.display.flip()
        clock.tick(30)  
    
    for each in all.sprites():
        each.kill()
    Menu(["rsc/bg/Level Select1.png"], size)
    Level1button = Button("rsc/bg/Level1screenshot.png", (128,230), (190,112))
    Level2button = Button("rsc/bg/level2screenshot.png", (392,230), (190,112))
    Level3button = Button("rsc/bg/level2screenshot.png", (656,230), (190,112))
    Level4button = Button("rsc/bg/Level4screenshot.png", (128,438), (190,112))
    Level5button = Button("rsc/bg/Level5screenshot.png", (392,438), (190,112))
    Level6button = Button("rsc/bg/Level6screenshot.png", (656,438), (190,112))
    

    
    while start2 and not start and not game:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                if Level1button.collidePoint(event.pos):
                    start = False    
                    start2 = False
                    game = True
                    level = 0
                    print level+1
                if Level2button.collidePoint(event.pos):
                    start = False    
                    start2 = False
                    game = True
                    level = 1
                    print level+1
                if Level3button.collidePoint(event.pos):
                    start = False
                    start2 = False
                    game = True
                    level = 2
                    print level+1
                if Level4button.collidePoint(event.pos):
                    start = False    
                    start2 = False
                    game = True
                    level = 3
                    print level+1
                if Level5button.collidePoint(event.pos):
                    start = False
                    start2 = False
                    game = True
                    level = 4
                    print level+1
                if Level6button.collidePoint(event.pos):
                    start = False
                    Start2 = False
                    game = True
                    level = 5
                    print level+1
                                         
        all.update(size,
                   player1.speedx, 
                   player1.speedy, 
                   player1.scrollingx, 
                   player1.scrollingy,
                   player1.realx,
                   player1.realy)
        dirty = all.draw(screen)
        pygame.display.update(dirty)
        pygame.display.flip()
        clock.tick(30)

          
    
    for each in all.sprites():
        each.kill()
    bg = Background("rsc/bg/mainbg.png", size)
    screen.blit(bg.image, bg.rect)
    loadLevel(levels[level])
    player1 = players.sprites()[0]
    
    while game:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    player1.direction("right")
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    player1.direction("left")
                if event.key == pygame.K_w or event.key == pygame.K_UP:
                    player1.direction("up")
                if event.key == pygame.K_s or event.key == pygame.K_DOWN:
                    player1.direction("down")
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    player1.direction("stop right")
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    player1.direction("stop left")
                if event.key == pygame.K_w or event.key == pygame.K_UP:
                    player1.direction("stop up")
                if event.key == pygame.K_s or event.key == pygame.K_DOWN:
                    player1.direction("stop down")
                
        playersHitBlocks = pygame.sprite.groupcollide(players, hardBlocks, False, False)
        playersHitEndBlocks = pygame.sprite.groupcollide(players, blocks, False, False)
        enemiesHitBlocks = pygame.sprite.groupcollide(enemies, hardBlocks, False, False)
        enemiesHitEnemies = pygame.sprite.groupcollide(enemies, enemies, False, False)
        playersHitEnemies = pygame.sprite.groupcollide(players, enemies, False, False)
              
        for player in playersHitBlocks:
            for block in playersHitBlocks[player]:
                player.collideBlock(block)
                
        for player in playersHitEndBlocks:
            for block in playersHitEndBlocks[player]:
                start = False
                game = False
                start2 = True
                
        for enemy in enemiesHitBlocks:
            for block in enemiesHitBlocks[enemy]:
                enemy.collideBlock(block)
                
        for enemy in enemiesHitEnemies:
            for otherEnemy in enemiesHitEnemies[enemy]:
                enemy.collideBlock(otherEnemy)
                
        for player in playersHitEnemies:
            for enemy in playersHitEnemies[player]:
                for each in all.sprites():
                    each.kill()
                bg = Background("rsc/bg/mainbg.png", size)
                screen.blit(bg.image, bg.rect)
                loadLevel(levels[level])
                player1 = players.sprites()[0]
        
        all.update(size,
                   player1.speedx, 
                   player1.speedy, 
                   player1.scrollingx, 
                   player1.scrollingy,
                   player1.realx,
                   player1.realy)
        
        dirty = all.draw(screen)
        pygame.display.update(dirty)
        pygame.display.flip()
        clock.tick(30)                
    














